package pyrachnid.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import pyrachnid.ctrl.Simulator;
import pyrachnid.view.component.RobotTypeListModel;
import pyrachnid.view.component.RobotTypeTableModel;

public class TypeRobotWindow extends JDialog {

	private static final long serialVersionUID = -4649325956904795985L;
	private final JPanel contentPanel = new JPanel();
	private Simulator _simulator;
	private JTable _table_typeRobotParams;
	private JList _list_typeRobot;

	/**
	 * Create the dialog.
	 */
	public TypeRobotWindow(Simulator p_sim) {

		_simulator = p_sim;

		setBounds(100, 100, 515, 506);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.X_AXIS));
		{
			JPanel panel_manage = new JPanel();
			contentPanel.add(panel_manage);
			panel_manage.setLayout(new GridLayout(0, 2, 0, 0));
			{
				_list_typeRobot = new JList();
				panel_manage.add(_list_typeRobot);
			}
			{
				JPanel panel_typeRobotParams = new JPanel();
				panel_manage.add(panel_typeRobotParams);
				panel_typeRobotParams.setLayout(new GridLayout(0, 1, 0, 0));
				{
					JScrollPane jscroll = new JScrollPane();
					panel_typeRobotParams.add(jscroll);
					jscroll.setLayout(new ScrollPaneLayout());
					{
						_table_typeRobotParams = new JTable();
						jscroll.setViewportView(_table_typeRobotParams);
					}
				}
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}

		this.initialize();
	}

	private void initialize() {
		_list_typeRobot.setModel(new RobotTypeListModel(_simulator.getSenario()
				.getRobotTypeList()));
		_list_typeRobot.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent evt) {
				JList list = (JList) evt.getSource();
				_table_typeRobotParams.setModel(new RobotTypeTableModel(
						_simulator.getSenario().getRobotTypeList()
								.get(list.getSelectedIndex())));
			}
		});
	}

}
